package com.somsoft.handjc.activity.tongji.xzzf;

import java.util.ArrayList;
import java.util.List;

import org.achartengine.ChartFactory;
import org.achartengine.chart.BarChart.Type;
import org.achartengine.chart.SuperChart;
import org.achartengine.renderer.SimpleSeriesRenderer;
import org.achartengine.renderer.XYMultipleSeriesRenderer;
import org.achartengine.renderer.XYMultipleSeriesRenderer.Orientation;
import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONArray;
import org.json.JSONObject;

import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.graphics.Color;
import android.graphics.Paint.Align;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.Window;
import android.widget.EditText;
import android.widget.Toast;

import com.somsoft.handjc.activity.MainActivity;
import com.somsoft.handjc.activity.R;
import com.somsoft.handjc.util.MyApplication;
import com.somsoft.handjc.util.Utils;
import com.somsoft.handjc.util.XzzfDataUtil;

/**
 * 部门移交立案数据对比
 *
 */
public class DeptYiJiaoDataChart extends SuperChart {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		requestWindowFeature(Window.FEATURE_NO_TITLE); 
		super.onCreate(savedInstanceState);
//		setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
		MyApplication.getInstance().addActivity(this);
		JSONArray array = new JSONArray();
		try {
			Intent it = getIntent();
			List<NameValuePair> params = new ArrayList<NameValuePair>();
			String strStart = it.getStringExtra("strStart");
			String strEnd = it.getStringExtra("strEnd");
        	
        	if(strStart!=null && !"".equals(strStart) && strEnd!=null && !"".equals(strEnd)){
				params.add(new BasicNameValuePair("common_dateStart",strStart));
				params.add(new BasicNameValuePair("common_dateEnd",strEnd));
			}
        	
			array = XzzfDataUtil.deptTransferLiAn(params);
		} catch (Exception e) {
			Log.e("DeptYiJiaoDataChart", e.getMessage(),e);
			Toast.makeText(DeptYiJiaoDataChart.this, "加载数据失败！", Toast.LENGTH_SHORT).show();
		}
		try{
			String[] titles = new String[] { "移交数量", "立案","不立案" };
			
		    List<double[]> values = new ArrayList<double[]>();
		    double[] yijiao = new double[array.length()];
		    double[] lian = new double[array.length()];
		    double[] bulian = new double[array.length()];
		    double tmpMax = 0;
		    for(int i=0;i<array.length();i++){
		    	JSONObject obj = array.getJSONObject(i);
	    		yijiao[i] = obj.getDouble("c_sum");
	    		lian[i] = obj.getDouble("filling");
	    		bulian[i] = obj.getDouble("notfilling");
	    		if(obj.getDouble("c_sum")>tmpMax){
	    			tmpMax = obj.getDouble("c_sum");
	    		}
		    }
		    values.add(yijiao);
		    values.add(lian);
		    values.add(bulian);
		    int[] colors = new int[] { Color.rgb(77, 198, 244),Color.rgb(255, 102, 102),Color.rgb(50, 176, 172) };
		    XYMultipleSeriesRenderer renderer = buildBarRenderer(colors);
		    renderer.setOrientation(Orientation.HORIZONTAL);
		    setChartSettings(renderer, "部门移交立案数据对比图", "", "", 0.5,
		        7, 0, tmpMax*1.1, Color.GRAY, Color.LTGRAY);
		    renderer.setXLabels(1);
		    renderer.setYLabels(10);
		    renderer.setBarSpacing(0.5);
		    
		   for(int i=0;i<array.length();i++){
			   JSONObject obj = array.getJSONObject(i);
			   String deptStr = obj.getString("deptName").replaceAll("江北区", "");
			   deptStr = splitDept(deptStr);
			   renderer.addXTextLabel(i+1, deptStr);
		   }
		    int length = renderer.getSeriesRendererCount();
		    for (int i = 0; i < length; i++) {
		      SimpleSeriesRenderer seriesRenderer = renderer.getSeriesRendererAt(i);
		      seriesRenderer.setChartValuesTextSize(30);
		      seriesRenderer.setChartValuesTextAlign(Align.RIGHT);
		      seriesRenderer.setDisplayChartValues(true);
		    }
		    
		    View bar = ChartFactory.getBarChartView(this, buildBarDataset(titles, values), renderer, Type.DEFAULT);
		    setContentView(bar);
		    Utils.closeLoading();
		} catch (Exception e) {
			Log.e("DeptYiJiaoDataChart", e.getMessage(),e);
		}
		setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
	}
	
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {			
		menu.add(Menu.NONE, 10, 10, "首页").setIcon(android.R.drawable.ic_menu_compass);
		menu.add(Menu.NONE, 15, 15, "列表").setIcon(android.R.drawable.ic_menu_info_details);
//		menu.add(Menu.NONE, 20, 20, "查询").setIcon(android.R.drawable.ic_menu_search);
		menu.add(Menu.NONE, 90, 90, "退出").setIcon(android.R.drawable.ic_menu_close_clear_cancel);
		return true;											
	}
	
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {		// 选中某个菜单项
		Intent it = null;
		switch (item.getItemId()) {								// 判断菜单项ID
		case  10:
			it = new Intent(DeptYiJiaoDataChart.this,MainActivity.class);
			startActivity(it);
			break;
		case  15:
			it = new Intent(DeptYiJiaoDataChart.this,XzzfTongJiMain.class);
			startActivity(it);
			break;
		case  20:
			LayoutInflater factory = LayoutInflater.from(this) ;
			View myView = factory.inflate(R.layout.common_date_start_end, null) ;
			final EditText start = (EditText)myView.findViewById(R.id.common_dateStart);
			final EditText end = (EditText)myView.findViewById(R.id.common_dateEnd);
			
			
			Dialog dialog = new AlertDialog.Builder(this)
				.setIcon(android.R.drawable.ic_menu_search) 
				.setTitle("请选择时间段")
				.setPositiveButton("确定", new DialogInterface.OnClickListener() {
					@Override
					public void onClick(DialogInterface dialog, int which) {
						try {
							String strStart = start.getText().toString();
							String strEnd = end.getText().toString();
							Intent it = new Intent(DeptYiJiaoDataChart.this,DeptYiJiaoDataChart.class);
							it.putExtra("strStart", formatDate(strStart));
							it.putExtra("strEnd", formatDate(strEnd));
							startActivity(it);
						} catch (Exception e) {
							Log.e("DeptYiJiaoDataChart", e.getMessage(),e);
						}
					}
				})
				.setNegativeButton("取消",new DialogInterface.OnClickListener(){
					@Override
					public void onClick(DialogInterface dialog, int which) {
						try {
							dialog.dismiss();
						} catch (Exception e) {
							Log.e("DeptYiJiaoDataChart", e.getMessage(),e);
						}
					}
					
				}).setView(myView).create();
			dialog.show() ;
			break;
		case  90:
			MyApplication.getInstance().exit();
			break;
		}
		return false;
	}
	
	private String formatDate(String date){
		//20120801转换为2012-08-01
		if("".equals(date)){
			return "";
		}
		String temp = "";
		temp += date.substring(0,4)+"-";
		temp += date.substring(4,6)+"-";
		temp += date.substring(6);
		return temp;
	}

}
